<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Category;
use app\model\Goods;
use app\model\Config;

class CategoryController extends Base
{
    public function list()
    {


        $ptype = input('get.ptype', '', 'serach_in');
        $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
        foreach ($data as &$vo) {
            $vo['image'] = toimg($vo['image']);
            $vo['icon'] = $vo['image'];
            $vo['name'] = $vo['title'];
        }

        return $this->json(['data' => $data]);
    }

    public function list2()
    {
        $ptype = input('get.ptype', '', 'serach_in');
        $topcid = input('get.topcid', '', 'serach_in');
        $data = Category::getlist(['pid' => $topcid, 'status' => 1, 'ptype' => $ptype]);
        return $this->json(['data' => $data]);
    }

    public function all()
    {
        $ptype = input('get.ptype', '', 'serach_in');
        $topcid = input('get.topcid', '', 'serach_in');
        $config = Config::getconfig('pagestyle');

        if (empty($config['categorystyle'])) {
            $config['categorystyle'] = 1;
        }

        if ($config['categorystyle'] == 1) {
            //$data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
            $data = Category::getcatapiall(0, $ptype);
        } elseif ($config['categorystyle'] == 2) {
            $data = Category::getcatapiall(0, $ptype);
        } elseif ($config['categorystyle'] == 3) {
            $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
            foreach ($data as &$vo) {
                $vo['image'] = toimg($vo['image']);
                $vo['goodslist'] = Goods::getGoodsBycat(['cat' => $vo['id'], 'count' => 100]);
            }
        } elseif ($config['categorystyle'] == 4) {
            $topdata = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
            foreach ($topdata as &$topvo) {
                $topvo['image'] = toimg($topvo['image']);
            }
            if (empty($topcid)) {
                $topcid = $topdata[0]['id'];
            }
            $data = Category::getlist(['pid' => $topcid, 'status' => 1, 'ptype' => $ptype]);
            foreach ($data as &$vo) {
                $vo['image'] = toimg($vo['image']);
                //$vo['goodslist'] = Goods::getGoodsBycat(['cat' => $vo['id'], 'count' => 100]);
            }
        }

        return $this->json(['data' => $data, 'topdata' => $topdata]);
    }

    public function technical()
    {
        $ptype = input('get.ptype', '', 'serach_in');
        $data = Category::getlist(['pid' => 0, 'status' => 1, 'ptype' => $ptype]);
        return $this->json(['data' => $data]);
    }

    public function demand()
    {
        $data = Category::getcatapiall(0, 2);
        return $this->json(['data' => $data]);
    }
}
